package com.ccb.jyzx.expand.tiantouonline.utils;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;

public class TargzUtil {

    static Logger logger= LoggerFactory.getLogger(TargzUtil.class);

    /**
     * @param sources 要打包的原文件数组
     * @param target  打包后的文件
     * @return File 返回打包后的文件
     * @throws
     * @Title: pack
     * @Description: 将一组文件打成tar包
     */
    public static File pack(List<File> sources, File target) throws IOException {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(target);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        TarArchiveOutputStream os = new TarArchiveOutputStream(out);
        for (File file : sources) {
            InputStream inputStream = null;
            try {
                //打包的时候只是包含文件，不带路径
                os.putArchiveEntry(new TarArchiveEntry(file, file.getName()));
                //os.putArchiveEntry(new TarArchiveEntry(file));  //打包的时候带路径
                inputStream = new FileInputStream(file);
                IOUtils.copy(inputStream, os);
                os.closeArchiveEntry();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
        if (os != null) {
            try {
                os.flush();
                os.close();
                logger.info("打包后文件为：" + target);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return target;
    }


    /**
     * tar打包压缩
     *
     * @param source   需要压缩的文件
     * @param FilePath 压缩后的文件全文件名(.tar)
     * @return 返回压缩后的文件
     */
    public static File compress(File source, String filePath) {
        File target = new File(filePath);
        FileInputStream in = null;
        GZIPOutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new GZIPOutputStream(new FileOutputStream(target));
            byte[] array = new byte[1024];
            int number;
            while ((number = in.read(array, 0, array.length)) != -1) {
                out.write(array, 0, number);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (in != null) {
                try {
                    in.close();
                    source.delete();//解压成功后，删除tar文件
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                    logger.info("打包后文件为：" + target);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return target;
    }

    public static void word2Tgz(String path,String target) {
        try {
            List<File> testFile = new ArrayList<>();
            List<File> listfiles = getFileList(path);

            for(File f:listfiles){
                testFile.add(f);
            }

            String temptarget = target.substring(0,target.lastIndexOf("."))+".tar";
            logger.info(temptarget);

            compress(pack(testFile, new File(temptarget)), target);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *      * 
     *      * @param strPath
     *      * @return
     *     
     */
    public static List<File> getFileList(String strPath) {
        List<File> filelist = new ArrayList<>();
        File dir = new File(strPath);
        // 该文件目录下文件全部放入数组
        File[] files = dir.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) {
                    //遍历子文件夹里面的东西
                    getFileList(files[i].getAbsolutePath());
                } else if (fileName.endsWith("exe")) {
                    // 以***结尾的文件
                    String strFileName = files[i].getAbsolutePath();
                    filelist.add(files[i]);
                } else {
                    filelist.add(files[i]);
                    continue;
                }
            }
        }
        return filelist;
    }


}